package com.fhz.a2102shopjava.qiniu;

import android.util.Log;

import com.qiniu.android.common.AutoZone;
import com.qiniu.android.common.FixedZone;
import com.qiniu.android.common.Zone;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;

import org.json.JSONObject;

/**
 * 时间:2023/8/18
 *
 * @author Mr.Feng
 * 简述: 七牛后端文件上传管理
 * 恶汉单例
 */
public class QiNiuUploadManager {
    private static final QiNiuUploadManager mQiNiuUploadManager = new QiNiuUploadManager();
    Configuration config;
    private UploadManager uploadManager;
    private QiNiuUploadManager(){
        config  =  new Configuration.Builder()
                .zone(FixedZone.zone2)
                .build();
        uploadManager  = new UploadManager(config);
    }

    public static QiNiuUploadManager getQiNiuUploadManager() {
        return mQiNiuUploadManager;
    }

    public UploadManager getUploadManager() {
        return uploadManager;
    }

    /**
     * 七牛后台上传的接口
     * @param data
     * @param fileName
     * @param token
     */
    public void updateFile(String data,String fileName,String token){
        uploadManager.put(data, fileName, token, (key, info, res) -> {
                    //res包含hash、key等信息，具体字段取决于上传策略的设置
                    if(info.isOK()) {
                        Log.i("qiniu", "Upload Success");
                        System.out.println(key);
                        System.out.println(info);
                        System.out.println(res);
                    } else {
                        Log.i("qiniu", "Upload Fail");
                        //如果失败，这里可以把info信息上报自己的服务器，便于后面分析上传错误原因
                    }
                    Log.i("qiniu", key + ",\r\n " + info + ",\r\n " + res);
                }, null);
    }
}   
